#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>


void dataLeft(int16_t pdta, int16_t *outData, uint8_t datalen) {
#if 0
    while(datalen > 1) {
        datalen--;
        outData[datalen] = outData[datalen - 1] ;
    }
    outData[0] = pdta;
#else
    uint8_t i = 0;
    for(i = 1; i < datalen; i++) {
        outData[i-1] = outData[i];
    }
    outData[datalen-1] = pdta;  
#endif
}
#if 1
int16_t CheckOrder(int16_t *array, uint16_t array_len, uint16_t min_diff) {
    uint8_t i = 0 ;
    int16_t ret = 0, half = 0, off = 0;
    //int16_t compr = 
    if(array_len < 2) {
        return 0;
    }
    half = array_len/2;
    off = array[0] - array[array_len - 1];
    if(off > min_diff) {
        for(i = 0; i < half; i++) {
            ret+= array[i] > array[half + i] ? 1 : 0;
        }
    }else if( off < -min_diff ) {
        for(i = 0; i < half; i++) {
            ret+= array[i] < array[half + i] ? -1 : 0;
        }
    }

    printf("off %d, ret =%d / %d\r\n", off, ret, (half/2));
    if(abs(ret) > (half / 2)) {
        //printf("ret = %d / %d\r\n", ret, (half*2/3));
        ret = ret > 0 ? 1 : -1;
    }else {
        ret = 0;
    }
    return ret;
}
#else

int16_t CheckOrder(int16_t *array, uint16_t array_len, uint16_t min_diff) {
    uint8_t i = 0 ;
    int16_t ret = 0, divide = 0, off = 0;
    int32_t section[2] = {0};
    if(array_len < 2) {
        return 0;
    }
    memset(section, 0, sizeof(section));
    divide = array_len / 2;

    print_array(array, 16);
    
    for(i = 0; i < divide; i++) {
        section[0]+= array[i] - array[0];
        section[1]+= array[divide + i] - array[0];
    }

    for(i = 0; i < 2; i++) {
        section[i] = section[i] / divide;
        printf("%03d ", section[i]);
    }
    printf("\r\n");
    if( section[1] + section[0] <  -500) {
        ret = 1;
    }else if( (section[1] - section[0]) < -200 && (section[0] > -100) ) {
        ret = 2;
    }
    if(ret != 0) {
        printf("ret = %d\r\n", ret);
    }
    return 0;
}


int16_t CheckOrder2(int16_t *array, uint16_t array_len, uint16_t min_diff) {
    uint8_t i = 0 ;
    int16_t ret = 0, divide = 0, off = 0;
    int32_t section[2] = {0};
    if(array_len < 2) {
        return 0;
    }
    memset(section, 0, sizeof(section));
    divide = array_len / 2;

    print_array(array, 16);
    
    for(i = 0; i < divide; i++) {
        section[0]+= array[i] - array[0];
        section[1]+= array[divide + i] - array[0];
    }

    for(i = 0; i < 2; i++) {
        section[i] = section[i] / divide;
        printf("%03d ", section[i]);
    }
    
    printf("\r\n");
    
    if( section[1] + section[0] <  -500) {
        ret = 1;
    }else if( (section[1] - section[0]) < -200 && (section[0] > -100) ) {
        ret = 2;
    }
    if(ret != 0) {
        printf("ret = %d\r\n", ret);
    }
    return 0;
}



#endif
static int16_t soft_stop(int16_t input, int16_t softd) {
    static int16_t tcnt = 0, last_ret = 0;
    int16_t ret = 0;
  
    if(1 == input && 0 == last_ret) {
        tcnt = softd;
    }
     
    if( 0 != tcnt ) {
        ret = 1;
        tcnt--;
    }

    last_ret = ret;
	printf("tnct %d\r\n", tcnt);
    return ret;
}


static int16_t soft_stop2(int16_t input, int16_t softd) {
    static int16_t tcnt = 0, last_ret = 0;
    int16_t ret = 0;
   int16_t raw = input;
    if(1 == input && 0 == last_ret) {
        tcnt = softd;
    }
     
    if( 0 != tcnt ) {
        input = 1;
        tcnt--;
    }

    last_ret = input;

	printf("tnct %d raw %d / %d\r\n", tcnt, raw, last_ret);
    return last_ret;
}


static int16_t soft_delay(int16_t input, int16_t softd, int16_t softc) {
    static int16_t tcnt = 0, last_ret = 0;
    int16_t ret = 0;
    static int16_t bsta = 0;
    switch(bsta) {
    case 1:
        if(0 != tcnt) {
            tcnt--;
            if(0 == tcnt) {
                tcnt = softc;
                bsta++;
            }
        }
        break;
    case 2:
        if(0 != tcnt) {
            tcnt--;
            if(0 == tcnt) {
                bsta = 0;
            }
        }
        break;
    case 0:    
    default:
        if(1 == input) {
            bsta++;
            tcnt = softd;
        }
        break;
    }

    if(1 == bsta) {
        ret = 1;
    }
	printf("ret %d, tnct %d\r\n", ret, tcnt);
    return ret;
}


#if 0
int16_t arrar[50] = {\
1, 0, 1, 0, 1, 1, 1, 1, 1, 1,\
1, 1, 1, 1, 1, 1, 1, 1, 1,\
};

#else


#endif
int16_t pe_a[20] = {0};

void print_array(int16_t *a, uint16_t len) {
    uint16_t i = 0;
    for(i = 0; i <len; i++) {
        printf("%02d ", a[i]);
    }
    printf("\r\n");
}

int16_t arrar[50] = {\
-20, -40, -60, -80, -100, -80, -60, -40, -20, 0,\
20, 40, 60, 40, 20, 0, -20, -40, -60,\
};

void test_soft(void) {
	int ss_t = 8;
	int16_t i = 0, output = 0;
	for(i = 0; i < 30; i++) {
		output = soft_delay( 20 * arrar[i], 8, 2);
		//output = arrar[i];
		
	}
	printf("\r\n");
}




int main(int argc, char** argv) {
	int ss_t = 10;
	if(argc > 1) {
		ss_t = atoi(argv[1]);
	}

    //test_soft();
    //return;
    srand(time(NULL));
    int i = 0;
    for(i = 0; i < 50 ; i++) {
        //arrar[i] = rand()%100 - i * 20;
        arrar[i] = 0 + i * 20;
#if 0
        arrar[15] = 1000;
        arrar[14] = 1000;
#endif  
    }
    for(i = 0; i < 30 ; i++) {
        dataLeft(arrar[i], pe_a, 20);
        if(i > 20) {
            
            int16_t ret = CheckOrder(pe_a, 20, 100);
            printf("[%d] ", ret);
            print_array(pe_a, 20);
        }
    }
	printf("\r\n");
}

